<template>
  <div>
    <Form
      oneitem="人员搜索"
      :select-show="false"
      :search-list="userList"
      :total-count="totalCount"
      :total-page="totalPage"
      :page-index="pageIndex"
      @changeList="hangeList"
      @searchResultFn="searchResultFn"
      @newCreateFn="dialogVisible = true"
    >
      <template #tableForm>
        <el-table-column label="序号" width="80" type="index" :index="indexMethod" />
        <el-table-column prop="userName" label="人员名称" width="312" />
        <el-table-column prop="regionName" label="归属区域" />
        <el-table-column prop="role.roleName" label="角色" />
        <el-table-column prop="mobile" label="联系电话" />
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.row)">修改</el-button>
            <el-button type="text" style="color:red" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </Form>
    <!-- 新建 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="630px" @closed="closeFn" @open="openFn">
      <el-form ref="userForm" :model="userForm" status-icon label-width="140px" :rules="userFormRules">
        <el-form-item label="人员名称" prop="userName">
          <el-input v-model="userForm.userName" type="text" placeholder="请输入" maxlength="5" show-word-limit />
        </el-form-item>
        <el-form-item label="角色：" prop="roleId">
          <el-select v-model="userForm.roleId" placeholder="请选择">
            <el-option v-for="item in roleOptions" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话：" prop="mobile">
          <el-input v-model="userForm.mobile" type="text" placeholder="请输入" maxlength="11" show-word-limit />
        </el-form-item>
        <el-form-item label="负责区域：" prop="regionId">
          <el-select v-model="userForm.regionId" placeholder="请选择" @change="selectFn">
            <el-option v-for="item in regionOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="头像：" prop="image">
          <!-- 图片上传组件 -->
          <UploadImg v-model="userForm.image" />
          <div style="font-size: 14px;color: #bac0cd; margin-top: 7px;">支持扩展名：jpg、png，文件不得大于100kb</div>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-checkbox v-model="userForm.status">是否启用</el-checkbox>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button id="buttom2" @click="dialogVisible=false">取 消</el-button>
        <el-button id="buttom1" type="primary" @click="createFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Form from '@/components/form/index.vue'
import { userSearch, role, regionSearch, addUser, userInfo, editUser, delUser } from '@/api'
export default {
  components: { Form },
  data() {
    return {
      userList: [],
      pageIndex: 1,
      totalPage: null,
      totalCount: null,
      dialogVisible: false,
      userForm: {
        userName: null,
        roleId: null,
        mobile: null,
        regionId: null,
        regionName: null,
        status: false,
        image: null
      },
      userFormRules: {
        userName: [
          { required: true, message: '请输入' }
        ],
        roleId: [
          { required: true, message: '请输入' }
        ],
        mobile: [
          { required: true, message: '请输入' },
          { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入正确手机号', trigger: 'change' }
        ],
        regionId: [
          { required: true, message: '请输入' }
        ],
        regionName: [
          { required: true, message: '请输入' }
        ],
        image: [
          { required: true, message: '请输入' }
        ]
      },
      roleOptions: [],
      regionOptions: []
    }
  },
  computed: {
    title() {
      return this.userForm.userId ? '编辑人员' : '新增人员'
    }
  },
  async created() {
    const res = await this.userSearchApi(this.pageIndex)
    this.userList = res.currentPageRecords
    this.totalCount = res.totalCount
    this.totalPage = res.totalPage
  },
  methods: {
    async userSearchApi(pageIndex, userName) {
      const res = await userSearch({
        pageIndex,
        userName
      })
      return res.data
    },
    indexMethod(index) {
      return this.pageIndex === 1 ? index + 1 : Number(`${this.pageIndex - 1}${index}`) + 1
    },
    async hangeList(page) {
      this.pageIndex = page
      const res = await this.userSearchApi(this.pageIndex)
      this.userList = res.currentPageRecords
    },
    async searchResultFn(form) {
      const res = await this.userSearchApi(1, form.taskCode)
      this.userList = res.currentPageRecords
    },
    async openFn() {
      const res = await role()
      const res1 = await regionSearch({ pageIndex: 1, pageSize: 1000 })
      console.log(res1)
      this.roleOptions = res.data
      this.regionOptions = res1.data.currentPageRecords
    },
    closeFn() {
      this.userForm = {
        userName: null,
        roleId: null,
        mobile: null,
        regionId: null,
        regionName: null,
        status: null,
        image: null
      }
      this.$refs.userForm.resetFields()
    },

    selectFn() {
      this.userForm.regionName = this.regionOptions.filter(item => item.id === this.userForm.regionId)[0].name
    },
    // 创建
    async createFn() {
      console.log(this.userForm.id)
      this.$refs.userForm.validate(async(valid) => {
        if (!valid) return
        if (this.userForm.userId) {
          const id = this.userForm.userId
          this.userForm = {
            userName: this.userForm.userName,
            roleId: this.userForm.roleId,
            mobile: this.userForm.mobile,
            regionId: this.userForm.regionId,
            regionName: this.userForm.regionName,
            status: this.userForm.status,
            image: this.userForm.image
          }
          await editUser(id, this.userForm).then(async() => {
            this.$message({
              type: 'success',
              message: '修改成功'
            })
          }).catch(res => {
            this.$message({
              type: 'warning',
              message: res.response.data
            })
          })
        } else {
          await addUser(this.userForm).then(async() => {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
          }).catch(res => {
            this.$message({
              type: 'warning',
              message: res.response.data
            })
          })
        }
      })
      const res = await this.userSearchApi(this.pageIndex)
      this.userList = res.currentPageRecords
      this.dialogVisible = false
    },
    async edit(row) {
      const res = await userInfo(row.id)
      this.userForm = res.data
      console.log(res.data)
      this.dialogVisible = true
    },
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delUser(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        const res = await this.userSearchApi(this.pageIndex)
        this.userList = res.currentPageRecords
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>

</style>
